<template>
  <div>
    <h1 style="border-bottom: 1px solid #ccc; padding-bottom: 10px; margin-bottom: 20px">
      个人资料
    </h1>
    <el-form
        ref="ruleFormRef"
        style="max-width: 600px"
        :model="ruleForm"
        :rules="rules"
        label-width="auto"
        class="demo-ruleForm"
        :size="formSize"
        status-icon
    >
      <div style="width: 80px;height: 80px;float: left;border-radius: 50%;overflow: hidden;">
        <img src="https://pic5.40017.cn/i/ori/1difaMet93G_90x90_00.jpg" alt="" width="80px" height="80px">
      </div>
      <div style="margin-left: 100px;width: 280px;height:110px">
        <h1>尊敬的会员大哥</h1>
        <span >欢迎您来到同程旅游，祝您旅途愉快！</span>
      </div>
      <el-form-item label="昵称" prop="name">
        <el-input v-model="ruleForm.nickName"  />
      </el-form-item>
      <el-form-item label="真实姓名" prop="username">
        <el-input v-model="ruleForm.realName" />
      </el-form-item>
      <el-form-item label="手机号码" prop="phone">
        <el-input v-model="ruleForm.phone" />
      </el-form-item>
      <el-form-item label="电子邮箱" prop="email">
        <el-input v-model="ruleForm.email" />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-radio-group v-model="ruleForm.sex">
          <el-radio value="1">男</el-radio>
          <el-radio value="0">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="职业" prop="region">
        <el-select v-model="ruleForm.career" placeholder="请选择职业">
          <el-option label="学生" value="xuesheng" />
          <el-option label="教师" value="jiaoshi" />
          <el-option label="公务员" value="jiaoshi" />
          <el-option label="人事/行政/前台/秘书" value="renshi" />
          <el-option label="金融/银行/保险" value="jinrong" />
          <el-option label="销售/采购" value="xiaoshou" />
          <el-option label="老板" value="boos" />
          <el-option label="自由职业" value="ziyou" />
          <el-option label="其他" value="qita" />

        </el-select>
      </el-form-item>
      <el-form-item label="出生日期" required>
        <el-col :span="11">
          <el-form-item prop="date1">
            <el-date-picker
                v-model="ruleForm.birthday"
                type="date"
                aria-label="Pick a date"
                placeholder="请选择出生日期"
                style="width: 100%"
            />
          </el-form-item>
        </el-col>


      </el-form-item>
      <el-form-item label="QQ号码" prop="QQID">
        <el-input v-model="ruleForm.qq" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm(ruleFormRef)">
          保存
        </el-button>
        <el-button @click="resetForm(ruleFormRef)">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts" setup>
import { useCounterStore } from '@/stores/counter';
import { reactive, ref ,onMounted} from 'vue'
import type { ComponentSize, FormInstance, FormRules } from 'element-plus'
import {getUserInfoById,updateUserInfo} from '@/api/zgr/myXinxi';
// 从pinia中获取用户id

// const counterStore = useCounterStore();
// 从Pinia中获取用户id
const id = useCounterStore().userForm.id;
// const id =localStorage.getItem('id');
const userId = ref(BigInt(id));
// 获取用户信息
const userInfo = async ()=>{
  console.log(userId.value);
  const res = await getUserInfoById(userId.value);
  console.log(res);
  //将user信息赋值给ruleForm
  ruleForm.nickName = res.data.data.nickName;
  ruleForm.realName = res.data.data.realName;
  ruleForm.phone = res.data.data.phone;
  ruleForm.email = res.data.data.email;
  ruleForm.sex = res.data.data.sex;
  ruleForm.career = res.data.data.career;
  ruleForm.qq = res.data.data.qq;
  ruleForm.birthday = res.data.data.birthday;
  ruleForm.id = res.data.data.id;
}

onMounted(()=>{
  userInfo();
})


interface RuleForm {
  nickName: string
  realName: string
  phone: string
  email: string
  birthday: string
  sex: boolean
  career: string
  qq: string
  // id: string
  id: string
}

const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<RuleForm>({
  nickName: '',
  realName: '',
  career: '',
  qq: "",
  email: "",
  phone: "",
  sex: false,
  birthday: "",
  id: ""
})

const locationOptions = ['Home', 'Company', 'School']

const rules = reactive<FormRules<RuleForm>>({
  nickName: [
    { required: true, message: '请输入你的昵称', trigger: 'blur' },
    { min: 3, max: 6, message: 'Length should be 3 to 5', trigger: 'blur' },
  ],
  realName: [
    { required: true, message: '请输入您的真实姓名', trigger: 'blur' },
    { min: 2, max: 5, message: 'Length should be 3 to 5', trigger: 'blur' },
  ],

  birthday: [
    {
      type: 'date',
      required: true,
      message: '请输入您的生日',
      trigger: 'change',
    },
  ],

})
//提交用户信息
const submitForm = async (formEl: FormInstance | undefined) => {
if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      //将ruleForm中的数据发送到后端
        updateUserInfo(ruleForm).then((res)=>{
          console.log(ruleForm)
          console.log(res)
      })
    } else {
      console.log('提交错误!', fields)
    }
  })
}

// const submitForm = async (formEl: FormInstance | undefined) => {
//   if (!formEl) return
//   await formEl.validate((valid, fields) => {
//     if (valid) {
//       console.log('提交成功!')
//     } else {
//       console.log('提交错误!', fields)
//     }
//   })
// }

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}

const options = Array.from({ length: 10000 }).map((_, idx) => ({
  value: `${idx + 1}`,
  label: `${idx + 1}`,
}))


















// import { ref, onMounted } from 'vue';
//
// const formData = ref({
//   username: '',
//   truename: '',
//   phone: '',
//   email: '',
//   radio1: '',
//   state1: '',
//   days: '',
//   QQ: ''
// });
//
// const submitForm = () => {
//   console.log(formData.value);
// };
//
// interface RestaurantItem {
//   value: string;
//   link: string;
// }
//
// const restaurants = ref<RestaurantItem[]>([]);
//
// const querySearch = (queryString: string, cb: any) => {
//   const results = queryString
//       ? restaurants.value.filter(createFilter(queryString))
//       : restaurants.value;
//   cb(results);
// };
//
// const createFilter = (queryString: string) => {
//   return (restaurant: RestaurantItem) => {
//     return restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0;
//   };
// };
//
// const loadAll = () => {
//   return [
//     { value: '学生', link: '' },
//     { value: '教师', link: '' },
//     { value: '公务员', link: '' },
//     { value: '人事/行政/前台/秘书', link: '' },
//     { value: '金融/银行/保险', link: '' },
//     { value: '销售/采购', link: '' },
//     { value: '老板', link: '' },
//     { value: '自由职业者', link: '' },
//     { value: '其他', link: '' },
//   ];
// };
//
// const handleSelect = (item: Record<string, any>) => {
//   console.log(item);
// };
//
// onMounted(() => {
//   restaurants.value = loadAll();
// });
</script>

<style scoped>
</style>